<?php
require_once("Service.php");
require_once("Database.php");

class Base extends Service {
	
	var $conn;
	
	/** The constructor of this object. */
	function __construct() {
		
		parent::__construct();
		$this->conn = new Database();
	}
	
	/**
	 * Attempts to log in a user using a username and password combination.
	 * @param $userName The user name of the user.
	 * @param $password The password of the user.
	 * @return A object containing user session data if the user exists, otherwise null.
	 */
	function loginUser($userName, $password) {
		
		$result = $this->conn->execute("
			SELECT userID, firstName, lastName, email, clearance
			FROM Users 
			WHERE userName = '" . md5($userName) . "'
			AND password = '" . md5($password) . "'
		");
		
		if(mysql_num_rows($result) == 1) {
		
			$row = mysql_fetch_assoc($result);
			
			$return = array();
			$return["sessionID"] = $this->allocateSession($row["userID"]);
			$return["userID"] = $row["userID"];
			$return["firstName"] = $row["firstName"];
			$return["lastName"] = $row["lastName"];
			$return["email"] = $row["email"];
			$return["clearance"] = $row["clearance"];
			$return["modules"] = $this->getModules($row["clearance"]);
			return $return;
			
		} else {
			
			return "";
		}
	}
	
	/**
	 * Attempts to register a user on the system.
	 * @param $userName The user name of the user.
	 * @param $password The password of the user.
	 * @param $firstName The first name of the user.
	 * @param $lastName The last name of the user.
	 * @param $email The email address of the user.
	 * @return The userID of the newly registered user, otherwise empty string.
	 */
	function registerUser($userName, $password, $firstName, $lastName, $email) {
		
		$result = $this->conn->execute("
			SELECT userID
			FROM Users 
			WHERE userName = '" . md5($userName) . "'
		");
		
		if(mysql_num_rows($result) == 0) {
			
			$rowID = $this->conn->insert("
				INSERT INTO Users(
					firstName,
					lastName,
					userName,
					password,
					email,
					clearance
				) VALUES(
					'" . $firstName . "',
					'" . $lastName . "',
					'" . md5($userName) . "',
					'" . md5($password) . "',
					'" . $email . "',
					1
				)
			");
			
			$return = array();
			$return["sessionID"] = $this->allocateSession($rowID);
			$return["userID"] = $rowID;
			$return["modules"] = $this->getModules(1);
			return $return;
			
		} else {
			
			return "";
		}
	}
	
	/**
	 * Terminates a user session.
	 * @param $sessionID The id of the session to terminate.
	 */
	function logoutUser($sessionID) {
		
		$this->terminateSession($sessionID);
	}
	
	/**
	 * Finds the modules for a certain clearance level.
	 * @param $clearnace The clearance level of the modules to find.
	 * @return An array containing the modules for the supplied clearance level.
	 */
	function getModules($clearnace) {
		
		$result = $this->conn->execute("
			SELECT
				moduleID,
				name,
				description,
				logo,
				url
			FROM Modules
			WHERE clearance <= " . $clearnace . "
		");
		
		$modules = array();
		while($row = mysql_fetch_assoc($result)) {
			$modules[] = $row;
		}
		return $modules;
	}
}
?>